<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-05 12:05:33
 * @LastEditTime: 2023-10-19 16:28:02
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;

use App\Http\Services\Service;
use App\Repositories\AttachmentRepository;
use App\Repositories\MpcCustAllocationRepository;
use App\Repositories\MpcCustAllocationDetailRepository;

use Illuminate\Support\Facades\DB;


class CustAllocationService extends Service
    {

    public $Detail;
    public $att;
    public function boot(MpcCustAllocationRepository $Repository, MpcCustAllocationDetailRepository $detail, AttachmentRepository $att)
        {
        $this->repository = $Repository;
        $this->Detail     = $detail;
        $this->att        = $att;
        }
    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function info($requset)
        {
        return $this->repository->condition($requset)->getOne();
        }

    public function details()
        {
        return $this->Detail->condition(request())->getGrid();
        }
    protected function save($request)
        {
        $ReqData = $request->toArray();
        // DB::enableQueryLog();
        // dd(DB::getQueryLog());

        $data = $this->repository->whereId($request->input("id"))->first();
        if ($data && $data->status_id > 0) {
            fail("已审核单据禁止编辑！");
            }
        // $this->repository->initializeData();

        $ReqData['quantity'] = $ReqData['auxiliary_quantity'] = 0;

        $ReqData['details'] = collect($ReqData['details'])->map(function ($row) use (&$ReqData)
            {
            if (
                ($row['company_id'] == 31 && $row['iscustomer'] == 0) || (
                    $row['company_id'] != 31 && $row['iscustomer'] == 1
                )
            ) {
                fail("必须客户调拨给YZ，或者YZ调拨给客户，禁止客户调拨给客户，或者YZ调拨给YZ");
                }
            $ReqData['quantity'] += $row['quantity'];
            $ReqData['auxiliary_quantity'] += $row['auxiliary_quantity'];

            return $row;
            });
        return $this->repository->autosave($ReqData);
        }
    protected function audit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 0)
                    fail("单据:[{$v->code}]已审核！");
                auditSave($v);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        $deltail->mergeWhere([
                            'iscustomer' => 1 - $deltail->iscustomer,
                        ])->addStorage("客户调拨入库", $v->code);
                        $deltail->mergeWhere([
                            'iscustomer'  => $deltail->iscustomer,
                            'customer_id' => ['key' => 'oldcompany_id'],
                            'company_id'  => $deltail->customer_id
                        ])
                            ->useStorage("客户调拨出库", $v->code, $v->quantity, $v->auxiliary_quantity, "old_mpc_detail");
                        $deltail
                            ->subStorage("old_mpc_detail");
                        }
                );
                });
        }
    protected function noaudit($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->get()
            ->filter(function ($v)
                {
                if ($v->status_id != 1)
                    fail("单据:[{$v->code}]未审核！");
                auditSave($v, false);
                $v->details->filter(
                    function ($deltail) use ($v)
                        {
                        $deltail->nosubStorage("old_mpc_detail");
                        $deltail->nouseStorage("old_mpc_detail");
                        $deltail->noaddStorage();
                        }
                );
                $v->save();
                });
        }
    protected function auditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->audit($ids);
        }
    protected function noauditItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->noaudit($ids);
        }
    protected function deleteItem()
        {
        $ids = $this->Detail->whereIn("id", $this->getRequestIds())->pluck("main_id");
        return $this->delete($ids);
        }
    protected function delete($ids = null)
        {
        return $this->repository
            ->whereIn("id", $ids ?? $this->getRequestIds())->with(['details'])
            ->autodelete()->filter(function ($row)
                {
                if ($row->status_id == 1)
                    fail("单据[{$row->code}]已审核！");
                return true;
                });
        }
    }